<template>
  <div class="ChildPage TablePage" v-loading="loading">
    <vb-page ref="page">
      <template #headerSearch-prepend="scope">
        <el-button size="small" v-loading="stockLoading" @click="reloadStock? getStockReloadResult(true): doStockSync()" :icon="reloadStock? 'el-icon-loading': 'el-icon-refresh'">{{ reloadStock? '同步中，点击刷新': '同步仓库' }}</el-button>
        <div style="flex-grow: 1; width: 100%;"></div>
      </template>
      <template #table-handle-prepend="scope">
        <el-button size="mini" type="text" icon="el-icon-edit" @click="gotoDetail(scope.row.fNumber)">编辑</el-button>
      </template>
      <template #table-fAllowMinusQty="scope">
        <el-tag :type="scope.row.fAllowMinusQty? 'success': 'danger'">{{ scope.row.fAllowMinusQty? "是": "否" }}</el-tag>
      </template>
    </vb-page>
  </div>
</template>

<script>
export default {
  data() {
    return {
      stockLoading: false,
      reloadStock: false,
    }
  },
  methods: {
    gotoDetail(fNumber = '') {
      let url = '/basic/stockDetail'
      if (fNumber) {
        url += `?fNumber=${ fNumber }`
      }
      this.$router.push(url)
    },
    /**
     * 获取加载按钮的加载中状态
     */
    async getStockReloadResult(showMsg) {
      this.stockLoading = true
      try {
        let res = await this.callApi("get_stock_reload_result")
        if (!res.res) return
        this.reloadStock = res.reloadStock
        this.reloadStockRes = res.reloadStockRes
        this.reloadStockMsg = res.reloadStockMsg
        this.reloadStockStartTime = res.reloadStockStartTime
        this.reloadStockEndTime = res.reloadStockEndTime
        if (showMsg) {
          if (this.reloadStock) {
            this.$message.info('仓库数据同步中')
          } else {
            if (this.reloadStockRes) {
              this.reload()
              this.$message.success('已同步完成, 请刷新最新结果')
            } else {
              this.$message.error('同步失败，' + this.reloadStockMsg)
            }
          }
        }
      } finally {
        this.stockLoading = false
      }
    },
    /**
     * 同步仓库
     * */
    async doStockSync() {
      try {
        let msg = '';
        if (this.reloadStockStartTime) {
          msg += `<span style="font-weight: bold;">同步仓库，耗时较长，是否继续执行？</span><br />
          前次同步${ this.reloadStockRes? '成功!': `<span style="color: var(--danger)">失败!</span>` } ${ this.reloadStockEndTime }
          ${ this.reloadStockRes? '': `<br />${ this.reloadStockMsg }` }
          <br />`;
        } else {
          msg = '同步仓库，耗时较长，是否继续执行？';
        }
        await this.$confirm(msg, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          dangerouslyUseHTMLString: true
        })
      } catch(e) {
        return
      }
      try {
        this.loading = true
        let res = await this.callApi("sync_stock")
        if (!res.res) return
        this.reloadStock = true
        this.$message.success('已经开始同步')
      } finally {
        this.loading = false
      }
    },
  }
}
</script>

<style scoped>

</style>
